﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Text;
using System.Web.SessionState;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.App;
using HKEA.CMS.Core.BLL.Mtbl;

namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// M_DeleteBrand 的摘要说明
    /// </summary>
    public class M_DeleteBrand : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            app_mtblBrandBLL app_brandBLL = new app_mtblBrandBLL();
            string strRet = string.Empty;
            var customerENT = BasePage.GetInstance().GetCustomerFromSession();
            if (customerENT != null)
            {
                string strKeyValue = context.Request.QueryString["UID"];
                if (!string.IsNullOrEmpty(strKeyValue))
                {
                    app_mtblBrand app_brand = new app_mtblBrand();
                    app_brand.CustomerUID = customerENT.CustomerUID;
                    string[] Info = strKeyValue.Split('-');
                    string From = Info[1];
                    int KeyUID = Convert.ToInt32(Info[0]);
                    if (From == "True")
                    {
                        app_brand.AppBrandUID = KeyUID;
                        app_brand.Deleted = 0;
                        app_brand = app_brandBLL.GetEntity(app_brand,true);
                        if (app_brand != null)
                        {
                            if (app_brand.BrandUID > 0)
                            {
                                app_brand.Deleted = 1;
                                if (app_brandBLL.UpdateENT(app_brand) > 0)
                                {
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                    WriteToLoge(app_brand.BrandName_Eng);
                                }
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                            else
                            {
                                if(app_brandBLL.DeleteENT(app_brand) > 0){
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                    WriteToLoge(app_brand.BrandName_Chi);
                                }
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                        }
                        else
                        { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}"; }
                    }
                    else
                    {
                        app_brand.BrandUID = KeyUID;
                        var tempappENT = app_brandBLL.GetEntity(app_brand, false);
                        if (tempappENT != null)
                        {
                            if (tempappENT.Deleted == 1)
                                strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}";
                            else
                            {
                                tempappENT.Deleted = 1;
                                if (app_brandBLL.UpdateENT(tempappENT) > 0){
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                    WriteToLoge(tempappENT.BrandName_Eng);
                                }
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                        }
                        else
                        {
                            mtblBrand tbl_brand = new mtblBrand();
                            mtblBrandBLL tbl_brandBLL = new mtblBrandBLL();
                            tbl_brand.Deleted = 0;
                            tbl_brand.BrandUID = KeyUID;
                            tbl_brand.CustomerUID = customerENT.CustomerUID;
                            tbl_brand = tbl_brandBLL.GetEntity(tbl_brand);
                            if(tbl_brand!=null)
                            {
                                app_brand.BrandUID = tbl_brand.CustomerUID;
                                app_brand.BrandLogo = tbl_brand.BrandLogo;
                                app_brand.BrandName_Chi = tbl_brand.BrandName_Chi;
                                app_brand.BrandName_Eng = tbl_brand.BrandName_Eng;
                                app_brand.CustomerUID =(int)tbl_brand.CustomerUID;
                                app_brand.Deleted = 1;
                                app_brand.Intro_Chi = tbl_brand.Intro_Chi;
                                app_brand.Intro_Eng = tbl_brand.Intro_Eng;
                                
                                if (app_brandBLL.InsertENT(app_brand) > 0){
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                    WriteToLoge(app_brand.BrandName_Eng);
                                }
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                            else
                            { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}"; }
                        }
                    }
                }
            }
            else
            { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200016") + "\"}"; }
            context.Response.Write(strRet);
        }
        protected void WriteToLoge(string BrandName)
        {
            tblHistory entlog = new tblHistory();
            BasePage page = new BasePage();
            tblCustomer Company = tblCustomerBLL.GetInstance().GetEntity(page.GetEnt());
            string LoginName = tblCust_WebLoginBLL.GetInstance().GetENT(page.GetMemberLoginUID()).LoginName.ToString();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 27;
            entlog.CustomerUID = Company.CustomerUID;
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = LoginName + " {" + Company.Name_Eng + "}{Brand}Delete" + BrandName;
            entlog.LastModifyBy = LoginName;
            int tal = HKEA.CMS.Core.BLL.Tbl.tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}